using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

namespace ReefinDotNet
{
    partial class Program
    {
        static void waterLowEvent(uint data1, uint data2, DateTime time)
        {
            if ((DateTime.Now - _lastFire).Duration() < WaitTime || _doneFilling == false || SumpLevel1.Read() != false || _ATOEnabled!=true)
            {
                //Debug.Print((DateTime.Now - _lastFire).Duration().ToString() + " - " + SumpLevel1.Read().ToString());
                return;
            }
            else
            {
                _doneFilling = false;
            }
            try
            {
                //LCD.write("ATO", 17, 3);
            }
            catch
            {
                Debug.Print("Couldn't Write to LCD");
                return;
            }
            try
            {
               ATO.Write(false);
            }
            catch
            {
                Debug.Print("Couldn't flip Relay");
                return;
            }
            while (SumpLevel1.Read() == false) //while still too low
            {
                Thread.Sleep(2000); //give it a 1 second pause so it doesn't flutter on / off
                //Debug.Print("still low on water, pump running!");
            }
            //finished filling, sensor says full.
            try
            {
                //LCD.write("   ", 17, 3);
            }
            catch
            {
            }
            try
            {
                ATO.Write(true);
            }
            catch
            {
                Debug.Print("Couldn't turn off pump!!");
                PowerState.RebootDevice(false);
            }
            _doneFilling = true;
            _lastFire = DateTime.Now;
        }
    }
}
